﻿using Core.Framework;
using HandlerContextOptions;
using Microsoft.AspNetCore.Http;
using System;
using System.IO;
using System.Threading.Tasks;

namespace Dashboard.Middlewares
{
    public class OnlineDevMiddlewares
    {
        private readonly RequestDelegate _next;
        const string devURL = "/dev";
        const string devHtml = "<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content=\"IE=edge\"><meta name=viewport content=\"width=device-width,initial-scale=1\"><link rel=icon href=/favicon.jpg><title>iTool-Online-Development</title><link href=/js/chunk-0aa60c40.3fb32320.js rel=prefetch><link href=/js/chunk-0b65ffb6.3515c674.js rel=prefetch><link href=/js/chunk-2d0a3577.103c8a47.js rel=prefetch><link href=/js/chunk-2d0a3cb6.5e3b236b.js rel=prefetch><link href=/js/chunk-2d0a43df.85cf4b06.js rel=prefetch><link href=/js/chunk-2d0aa90c.eb5ec9cf.js rel=prefetch><link href=/js/chunk-2d0aab07.544fce83.js rel=prefetch><link href=/js/chunk-2d0abc00.a982961f.js rel=prefetch><link href=/js/chunk-2d0ae937.293ae831.js rel=prefetch><link href=/js/chunk-2d0aeb45.6f6c95fd.js rel=prefetch><link href=/js/chunk-2d0af08c.ffa123d5.js rel=prefetch><link href=/js/chunk-2d0afa49.3aa985e2.js rel=prefetch><link href=/js/chunk-2d0b2762.9922ab75.js rel=prefetch><link href=/js/chunk-2d0b6187.46d183b8.js rel=prefetch><link href=/js/chunk-2d0ba136.8f5ef0f9.js rel=prefetch><link href=/js/chunk-2d0bb267.a9fb13e9.js rel=prefetch><link href=/js/chunk-2d0bdf38.9d1c0afd.js rel=prefetch><link href=/js/chunk-2d0bff92.7e3f2915.js rel=prefetch><link href=/js/chunk-2d0c0494.6cdb553e.js rel=prefetch><link href=/js/chunk-2d0c0a09.2ec1ba15.js rel=prefetch><link href=/js/chunk-2d0c4313.54e0193b.js rel=prefetch><link href=/js/chunk-2d0c46d1.524003d3.js rel=prefetch><link href=/js/chunk-2d0c512b.aee55b70.js rel=prefetch><link href=/js/chunk-2d0cbca2.5cddaad9.js rel=prefetch><link href=/js/chunk-2d0cbd03.035fd695.js rel=prefetch><link href=/js/chunk-2d0cf16e.3c832286.js rel=prefetch><link href=/js/chunk-2d0cf2c6.5514ccb3.js rel=prefetch><link href=/js/chunk-2d0d056d.6cd4fbb2.js rel=prefetch><link href=/js/chunk-2d0d0645.329dfb61.js rel=prefetch><link href=/js/chunk-2d0d2f22.2efdeec4.js rel=prefetch><link href=/js/chunk-2d0d5cd7.05c53b89.js rel=prefetch><link href=/js/chunk-2d0d61fd.bb176310.js rel=prefetch><link href=/js/chunk-2d0d7e63.19f60769.js rel=prefetch><link href=/js/chunk-2d0e1b57.068930de.js rel=prefetch><link href=/js/chunk-2d0e1fbe.c78950d1.js rel=prefetch><link href=/js/chunk-2d0e22d6.4275622b.js rel=prefetch><link href=/js/chunk-2d0e542a.99458ba9.js rel=prefetch><link href=/js/chunk-2d0e57ed.ff4a3771.js rel=prefetch><link href=/js/chunk-2d0e5eb7.752351b7.js rel=prefetch><link href=/js/chunk-2d0e6553.326a5893.js rel=prefetch><link href=/js/chunk-2d0e6c86.1cade724.js rel=prefetch><link href=/js/chunk-2d0ea098.6cab0e15.js rel=prefetch><link href=/js/chunk-2d0f0a11.864b636f.js rel=prefetch><link href=/js/chunk-2d208ac5.fa7099bf.js rel=prefetch><link href=/js/chunk-2d209408.0fb90cc2.js rel=prefetch><link href=/js/chunk-2d20f745.69222c89.js rel=prefetch><link href=/js/chunk-2d20ff23.5e0146c6.js rel=prefetch><link href=/js/chunk-2d2138c7.b4921213.js rel=prefetch><link href=/js/chunk-2d216f3b.5a89b9b2.js rel=prefetch><link href=/js/chunk-2d217e5b.041d0b9f.js rel=prefetch><link href=/js/chunk-2d21842a.0092ef43.js rel=prefetch><link href=/js/chunk-2d21b84a.ebafb1c6.js rel=prefetch><link href=/js/chunk-2d21dcd2.e91e53ec.js rel=prefetch><link href=/js/chunk-2d21f327.25a54bd8.js rel=prefetch><link href=/js/chunk-2d2214b3.3a6a73d6.js rel=prefetch><link href=/js/chunk-2d221799.3134e208.js rel=prefetch><link href=/js/chunk-2d221814.710d412d.js rel=prefetch><link href=/js/chunk-2d221a34.01bf253f.js rel=prefetch><link href=/js/chunk-2d22502a.1d827e48.js rel=prefetch><link href=/js/chunk-2d226775.968713fa.js rel=prefetch><link href=/js/chunk-2d229411.22b01025.js rel=prefetch><link href=/js/chunk-2d2295e9.92fd756e.js rel=prefetch><link href=/js/chunk-2d22c171.404cff55.js rel=prefetch><link href=/js/chunk-2d22c2b8.e6838beb.js rel=prefetch><link href=/js/chunk-2d22c360.fd7576d1.js rel=prefetch><link href=/js/chunk-2d22ca58.56109482.js rel=prefetch><link href=/js/chunk-2d2311f7.536ce1ec.js rel=prefetch><link href=/js/chunk-2d237ee7.5b7e6771.js rel=prefetch><link href=/js/chunk-2d238465.52bb5089.js rel=prefetch><link href=/js/chunk-30597b4a.36ce1551.js rel=prefetch><link href=/js/chunk-35886eb6.14fe21ec.js rel=prefetch><link href=/js/chunk-35a285bb.6f8b7f8f.js rel=prefetch><link href=/js/chunk-628cd425.5a2cb1ca.js rel=prefetch><link href=/js/chunk-746e09c3.ab6605fd.js rel=prefetch><link href=/js/chunk-74bca5c7.55173274.js rel=prefetch><link href=/js/chunk-7532b3ea.3af1886d.js rel=prefetch><link href=/js/chunk-a94243e2.5f14c71d.js rel=prefetch><link href=/js/chunk-dec07a14.f6012039.js rel=prefetch><link href=/js/chunk-e13e4362.ed68ea6f.js rel=prefetch><link href=/js/app.0593bc59.js rel=preload as=script><link href=/js/chunk-vendors.e4f832ce.js rel=preload as=script></head><body><noscript><strong>We're sorry but iTool-Online-Development doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id=app></div><script src=/js/chunk-vendors.e4f832ce.js></script><script src=/js/app.0593bc59.js></script></body></html>";

        public OnlineDevMiddlewares(RequestDelegate next)
        {
            this._next = next ?? throw new ArgumentNullException(nameof(next));
        }

        public async Task Invoke(HttpContext context)
        {
            bool isDevPath = context.Request.Path.Value == devURL || context.Request.Path.Value.StartsWith(devURL + "/");

            if (isDevPath)
            {
                var cookiesKey = DESEncrypt.Encrypt(typeof(UserLoginResult).Name);
                // 验证是否登录
                if (context.Request.Cookies.ContainsKey(cookiesKey))
                {
                    context.Request.Cookies.TryGetValue(cookiesKey, out string value);

                    if (!string.IsNullOrWhiteSpace(value))
                    {
                        var user = DESEncrypt.Decrypt(value).TryToEntity<UserLoginResult>() ?? new UserLoginResult();
                        if (user.UserKey > 0)
                        {
                            context.Response.ContentType = "text/html";
                            await context.Response.WriteAsync(devHtml, context.RequestAborted);
                            return;
                        }
                    }

                }
            }

            await this._next(context);
        }

    }
}
